package calculator.model;/*
 * @author     ：zhangbo
 * @date       ：Created in 2021/5/21 21:02
 * @description：封装计算器的计算模型
 * @modified By：zhangbo
 * @version:     $
 */

import javax.swing.*;
import java.util.Stack;

public class Computer {
    public double result;   //运算结果
    public Stack<Character> dataItem; //输入的值
    public Stack<Double> tempResult; //临时结果值
    public Stack<Character> operator; //操作符

    public Computer() {
        init();
    }

    public void init() {
        dataItem = new Stack<Character>();
        tempResult = new Stack<Double>();
        //dataItem.push('0');
        operator = new Stack<Character>();
    }

    public void setDataItem(char c) {
        if (c != '.')
            dataItem.push(c);
        else {
            if (!dataItem.contains('.'))
                dataItem.push(c);
        }
    }

    public void setOperator(char p) {
        if (dataItem.empty()) {
            operator.clear();
            operator.push(p);
            return;
        }
        if (operator.empty()) {
            operator.push(p);
            double m = computerDataItem();
            dataItem.removeAllElements();
            tempResult.push(m);
        } else {
            double mData = computerDataItem();
            dataItem.removeAllElements();
            char yuansuan = operator.pop();
            double rTemp = tempResult.pop();
            if (yuansuan == '+')
                rTemp = rTemp + mData;
            else if (yuansuan == '-')
                rTemp = rTemp - mData;
            else if (yuansuan == '/')
                rTemp = rTemp / mData;
            else if (yuansuan == '*')
                rTemp = rTemp * mData;
            tempResult.push(rTemp);
            operator.push(p);
        }
    }

    public char getOperator() {
        if (operator.empty()) {
            return '\0';
        }
        return operator.peek();
    }

    public void backspace() {
        if (dataItem.size() >= 1) {
            dataItem.pop();
        }
    }

    public void initDataItemByMath(MathComputer computer) {
        computer.handle(this);
    }

    public double getResult() {
        double endItem = 0;
        if (dataItem.empty()) {
            if (!tempResult.empty()) {
                endItem = tempResult.peek();
            }
        } else {
            endItem = computerDataItem();
        }

        if (operator.empty()) {
            result = endItem;
            return result;
        }
        char yunsuan = operator.peek();
        if ((yunsuan == '+') && (!tempResult.empty())) {
            result = tempResult.peek() + endItem;
        } else if (yunsuan == '-') {
            result = tempResult.peek() - endItem;

        } else if (yunsuan == '*') {
            result = tempResult.peek() * endItem;
        } else if (yunsuan == '/') {
            if (endItem == 0) {
                JOptionPane.showMessageDialog(null, "除数不可以为0", "error", JOptionPane.WARNING_MESSAGE);
                result = 0;
            } else {
                result = tempResult.peek() / endItem;
            }

        }
        tempResult.clear();
        tempResult.push(result);
        return result;
    }

    public double getTempResult() {
        double r = 0;
        if (tempResult.empty()) {
            r = computerDataItem();
        } else {
            r = tempResult.peek();
        }
        return r;
    }

    public double computerDataItem() {
        if (dataItem.empty() && !tempResult.empty()) {
            return tempResult.peek();
        }
        StringBuilder str = new StringBuilder();
        double doubleData = 0;
        for (Character character : dataItem) {
            str.append(character);
        }
        try {
            doubleData = Double.parseDouble(str.toString());
        } catch (NumberFormatException e) {
            doubleData = 0;
        }
        return doubleData;
    }
}